#include "gamer.h"
#include "ui_gamer.h"

Gamer::Gamer(QWidget *parent) :
  QWidget(parent),
  ui(new Ui::Gamer)
{
  ui->setupUi(this);
  setFixedSize(750,750);
  myboard=new ChessBoard();
  judgeman=new Judge();
}

Gamer::~Gamer()
{
  delete ui;
}

void Gamer::mousePressEvent(QMouseEvent *e)
{
	int xx,yy;
	//判断落入
	xx=e->pos().x()/(width()/15);
	yy=e->pos().y()/(width()/15);
	if(myboard->count%2==0)
	  {
		myboard->block[xx][yy].setType(WHITE);

	  }
	else {
		myboard->block[xx][yy].setType(BLACK);
	}
	myboard->count++;
	update();
}

void Gamer::PaintEvent(QPaintEvent *e)
{
	//定义一个画家
	QPainter *painter = new QPainter(this);
	//通用画家的drawLine方法依次画横竖各15条线组成棋盘网格
	for (int i=0;i<=15;i++){
			painter->drawLine(0,i*this->width()/15.0,this->width(),i*this->height()/15.0);
		}
	for (int i=0;i<=15;i++){
			painter->drawLine(i*this->height()/15.0,0,i*this->width()/15.0,this->width());
		}
	//定义一只画笔，给画笔设置样式(颜色、线条宽度、线条类型)，并把画笔给画家
	QPen pen;
	pen.setColor(Qt::black);
	painter->setPen(QPen(Qt::black));
	//查看当前棋盘上棋子信息，当存在棋子时，以网格坐标为准推导绘制位置，绘制相应的棋子
	for (int i=0;i<15;i++){
			for (int j=0;j<15;j++){
					if(myboard->block[i][j].getType()==WHITE)
						{
							painter->setBrush(Qt::NoBrush);
							painter->drawEllipse(i*width()/15.0,j*height()/15.0,width()/15.0,height()/15.0);
						}
				}
		}
	for (int i=0;i<15;i++){
			for (int j=0;j<15;j++){
					if(myboard->block[i][j].getType()==BLACK)
						{
							painter->setBrush(QColor(Qt::black));
							painter->drawEllipse(i*width()/15.0,j*height()/15.0,width()/15.0,height()/15.0);
						}
				}
		}
}

